<!DOCTYPE html>
<html lang="en" class="x-admin-sm" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>我的联盟</title>
    <header th:replace="header::html"></header>
</head>
<body>
<div class="demoTable">
    <div class="layui-fluid">
        <div class="layui-row  layui-col-space15">
            <div class="layui-col-md12">
                <div class="layui-card">
                    <div class="layui-card-body ">
                        <div class="layui-inline">
                            联盟名称:
                            <div class="layui-inline">
                                <input type="text" id="aname"  autocomplete="off"
                                       class="layui-input">
                            </div>
                        </div>
                        <div class="layui-inline layui-show-xs-block">
                            <button class="layui-btn" data-type="reload">查询</button>
                            <input type="button" class="layui-btn layui-btn-danger layui-btn-xs" value="返回主页" onclick="back()">

                        </div>
                    </div>
                    <div class="layui-card-body ">
                        <table id="demo" lay-filter="test"></table>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>
<!--<div class="demoTable">-->
<!--    联盟名称:<input  id="aname">-->
<!--    <button class="layui-btn" data-type="reload">查询</button>-->
<!--    <input type="button" class="layui-btn layui-btn-danger layui-btn-xs" value="返回主页" onclick="back()">-->
<!--</div>-->

<!--<table id="demo" lay-filter="test"></table>-->

<script type="text/html" id="msg">
    <table id="table" lay-filter="test2"></table>
</script>

<script type="text/html" id="msg2">
    <table id="table2" lay-filter="test3"></table>
</script>

<script type="text/html" id="bar">
    {{#  if(d.tips1==='盟主'){ }}
    <a class="layui-btn layui-btn-xs" lay-event="edit">申请详情</a>
    <a class="layui-btn layui-btn-xs" lay-event="look">查看联盟</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">解散联盟</a>
    {{#  } }}
    {{#  if(d.tips2==='运行中'){ }}
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="back">我要退出</a>
    {{#  } }}
    {{#  if(d.tips2==='待审核'){ }}
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="no">撤回申请</a>
    {{#  } }}
</script>

<script type="text/html" id="bar2">
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="run">踢出联盟</a>
</script>

<script type="text/html" id="bar3">
    <a class="layui-btn layui-btn-xs" lay-event="agree">同意申请</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="res">拒绝申请</a>
</script>
<script>

	function back()
	{
		window.parent.location.href="/api-front/getPage?pageName=index";
	}

	layui.use('table', function(){
		var table = layui.table;
		table.render({
			elem: '#demo'
			,type:'POST'
			,url: '/api-front/alliance/getList2'
			,page: true
			,limit:5
			,limits:[5]
			,id:"testReload"
			,cols: [
				[
					{field: 'aid', hide:true}
					,{field: 'aaccout', hide:true}
					,{field: 'aname', title: '联盟名称', width:200}
					,{field: 'ainfo', title: '联盟简介', width:200}
					,{field: 'tips3', title: '联盟类型', width:200}
					,{field: 'amargin', title: '入会费用', width:200}
					,{title:'操作',width:250,toolbar:"#bar"}
				]
			]
		});
		$('.demoTable .layui-btn').on('click', function ()
		{
			var type = $(this).data('type');
			if (type == 'reload')
			{
				//执行重载
				table.reload('testReload',
					{
						page:
							{
								curr: 1
							}
						, where:
							{
								aname: $("#aname").val()
							}
					});
			}
		});


		table.on('tool(test)', function(obj)
		{
			var data = obj.data;
			var layEvent = obj.event;
			var tr = obj.tr;
			if(layEvent === 'del')
			{
				layer.confirm('确定要解散该联盟吗?', function(index)
				{
					$.ajax({
						type:"POST",
						url:'/api-front/alliance/deleteAlliance',
						dataType:"text",
						data:{aid:data.aid,money:data.amargin,account:data.aaccout},
						success:function(msg)
						{
							if(msg==="yes")
							{
								layer.alert("解散成功", {icon: 1});
								$(".layui-laypage-btn")[0].click();
								layer.close(index);
							}
							else
							{
								layer.alert("该联盟有未完成的订单，暂时无法解散", {icon: 5});
							}
						},
						error:function ()
						{
							layer.alert("服务器正忙.....", {icon: 5});
						}
					});
				});
            }
			else if(layEvent === 'edit')
			{
				var table2= layui.table;
				var str=layer.open({
					type: 1,
					content: $('#msg').html(),
					area: ['800px','500px'],
					title: '申请列表',
					btn:['返回'],
					success: function() {
						table2.render({
							elem: $('#table')
							,type:'POST'
							,url: '/api-front/alliance/getUsers2'
							,page: true
							,where:{aid:data.aid}
							,limit:5
							,limits:[5]
							,cols: [
								[
									{field: 'uid', hide:true}
									,{field: 'uname', title: '用户名', width:200}
									,{field: 'uscore', title: '信用分', width:200}
									,{field: 'tips1', title: '服务类型', width:200}
									,{title:'操作',width:180,toolbar:"#bar3"}
								]
							]
						});
						table.on('tool(test2)', function(obj) {
							var da = obj.data;
							var lay = obj.event;
							var type='拒绝';
							if(lay==='agree')
                            {
                            	type='同意';
                            }
							layer.confirm('确定要'+type+'吗?', function(index2)
							{
								$.ajax({
									type:"POST",
									url:'/api-front/alliance/updateState',
									dataType:"text",
									data:{aid:data.aid,money:data.amargin,uid:da.uid,type:type,account:data.aaccout},
									success:function(msg)
									{
										if(msg==="yes")
										{
											layer.alert("操作成功", {icon: 1});
											layer.close(str);
										}
										else
										{
											layer.alert("操作失败，请重试", {icon: 5});
										}
									},
									error:function ()
									{
										layer.alert("服务器正忙.....", {icon: 5});
									}
								});
							});
						});
					}
				});
			}
			else if(layEvent === 'look')
            {
	            var table2= layui.table;
			            var str=layer.open({
				            type: 1,
				            content: $('#msg2').html(),
				            area: ['800px','500px'],
				            title: '我的联盟',
				            btn:['返回'],
				            success: function() {
					            table2.render({
						            elem: $('#table2')
						            ,type:'POST'
						            ,url: '/api-front/alliance/getUsers'
						            ,page: true
                                    ,where:{aid:data.aid}
						            ,limit:5
						            ,limits:[5]
						            ,cols: [
						            	[
								            {field: 'uid', hide:true}
								            ,{field: 'uname', title: '用户名', width:200}
								            ,{field: 'uscore', title: '信用分', width:200}
								            ,{field: 'tips1', title: '服务类型', width:200}
								            ,{title:'操作',width:150,toolbar:"#bar2"}
                                    ]
                                    ]
					            });
					            table.on('tool(test3)', function(obj) {
						            var d = obj.data;
						            layer.confirm('确定要将'+d.uname+'踢出联盟吗?', function(index3)
						            {
							            $.ajax({
								            type:"POST",
								            url:'/api-front/alliance/outAlliance',
								            dataType:"text",
								            data:{aid:data.aid,money:data.amargin,uid:d.uid},
								            success:function(msg)
								            {
									            if(msg==="yes")
									            {
										            layer.alert("操作成功", {icon: 1});
										            layer.close(str);
									            }
									            else
									            {
										            layer.alert("操作失败，请重试", {icon: 5});
									            }
								            },
								            error:function ()
								            {
									            layer.alert("服务器正忙.....", {icon: 5});
								            }
							            });
                                    });
					            });
				            }
			            });
            }
			else if(layEvent === 'back' || layEvent === 'no')
            {
            	var str='撤回申请';
            	if(layEvent === 'back')
                {
	                str='退出该联盟';
                }
	            layer.confirm('确定要'+str+'吗?', function(index)
	            {
		            $.ajax({
			            type:"POST",
			            url:'/api-front/alliance/backAlliance',
			            dataType:"text",
			            data:{aid:data.aid,money:data.amargin},
			            success:function(msg)
			            {
				            if(msg==="yes")
				            {
					            layer.alert("操作成功", {icon: 1});
					            $(".layui-laypage-btn")[0].click();
					            layer.close(index);
				            }
				            else
				            {
					            layer.alert("操作失败，请重试", {icon: 5});
				            }
			            },
			            error:function ()
			            {
				            layer.alert("服务器正忙.....", {icon: 5});
			            }
		            });
	            });
            }
		});

	});

</script>

</body>
</html>